<?php
/**
 * 物流信息
 * Created by PhpStorm.
 * User: jiayanxiu
 * Date: 15/11/10
 * Time: 17:04
 */
class LogisticsApp extends MallbaseApp
{

    private $method;
    private $user_id;
    private $url = 'http://m.heshetrends.com/';

    /**
     * LogisticsApp constructor
     */
    public function __construct() {
        $this->method = strtoupper($_SERVER['REQUEST_METHOD']);
        $this->user_id = $this->checkLogin();

    }

    /**
     * 物流列表
     * @author jyx
     * @date 2015/11/10
     */
    public function index() {
        $action = '物流列表';
        setLogParameter($action);
        if($this->method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法的请求!');
        }
        if (!$this->user_id) {
            $this->ajax_return(ERROR_LOGIN, '请您登陆!');
        }
        $order_id = $this->get_gp('order_id', 'G');
        $order_id = $order_id ? intval($order_id) : 0;
        if (!$order_id) {
            $this->ajax_return(FLAG_ERROR, '订单信息不存在!');
        }
        $model = m('order');
        //查询出来快递公司
        $sql = "SELECT o.invoice_no,se.id,se.name,se.img,se.uri,se.daima FROM ".DB_PREFIX."order_extm oe
            LEFT JOIN `".DB_PREFIX."order` o ON oe.order_id=o.order_id LEFT JOIN ".DB_PREFIX."ship_express se
            ON oe.shipping_id=se.id WHERE oe.order_id={$order_id}";
        $express = $model->getRow($sql);
        //查询快递信息
        $param = array();
        $param['sn'] = $express['invoice_no'];
        $param['type'] = $express['daima'];
        $info = $this->aicha($param);
        if ($info) {
            $express['img'] = $express['img'] ? getUrlByEnv($express['img']) : '';
            $address = array();
            foreach($info as $k=>$v){
                $address[$k]['time']=$v['time'];
                $address[$k]['context'] = $v['content'];
            }
            $data[0] = $express;
            $data[1] = $address;
            $this->ajax_return(FLAG_OK, '', $data);
        } else {
            $this->ajax_return(FLAG_ERROR, '暂无物流信息!');
        }
    }

    /**
     * @param $type
     *
     * 因为数据库中已经存在一个快递数据列表了，所以类型可能不一致
     * 这里有个对应的转换处理
     *
     * 默认他们的识别类型是一样的。
     *
     * key[old]=>value[new]
     *
     */
    protected function gettype($type)
    {
        $data = array();

        $data['youshuwuliu'] = 'yousu';//优速快递
        $data['debangwuliu'] = 'debang';//德邦物流

        if(isset($data[$type]))
        {
            return $data[$type];
        }

        return $type;
    }



    //爱查询的数据处理
    /**
     * 爱查询数据处理
     *
     * http://www.aikuaidi.cn/query/
     *
     * 查询代码，
     * http://www.aikuaidi.cn/api/?type=2
     */
    public function aicha($param)
    {
        $action = '爱查询数据处理';
        setLogParameter($action);
        $url = "http://www.aikuaidi.cn/query/";

        $data = array();
        $data['id'] = $this->gettype($param['type']);
        $data['order'] = $param['sn'];

        $ch = curl_init ();


        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

        curl_setopt ($ch,CURLOPT_REFERER,'http://www.aikuaidi.cn');

        curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");

        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        $return = curl_exec ( $ch );


        curl_close ( $ch );


        $json = json_decode($return,true);

        if(isset($json['errcode']) && $json['errcode'] == '0000')
        {
            return $json['data'];
        }
        else
        {
            return array();
        }


    }




}
